<?php

namespace app\home\controller;

use app\common\model\Goods;
use think\Lang;
use think\Db;


/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class Importgoods extends BaseSeller
{
    protected $template_dir;
    var $_store_id;

    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'home/lang/' . config('default_lang') . "/sellergoodsadd.lang.php");
        $this->template_dir = 'default/seller/importgoods/';
    }

    public function index()
    {
        $this->goods_list();
    }

    /**
     * 供应商出售中的商品列表
     */
    public function goods_list()
    {
        $goods_model = model('goods');

        $where = array();
        $storegc_id = intval(input('get.storegc_id'));

        if ($storegc_id > 0) {
            $where['goods_stcids'] = array('like', '%,' . $storegc_id . ',%');
        }

        //筛选分类 品牌
        $b_id = intval(input('get.b_id'));
        if ($b_id != '') {
            $where['brand_id'] = intval($b_id);
        }
        $gc_id = intval(input('get.gc_id'));
        if ($gc_id != '') {
            $where["gc_id|gc_id_1|gc_id_2|gc_id_3"] = $gc_id;
        }

        $keyword = trim(input('get.keyword'));
        $search_type = trim(input('get.search_type'));
        if (trim($keyword) != '') {
            switch ($search_type) {
                case 0:
                    $where['goods_name'] = array('like', '%' . trim($keyword) . '%');
                    break;
                case 1:
                    $where['goods_serial'] = array('like', '%' . trim($keyword) . '%');
                    break;
                case 2:
                    $where['goods_commonid'] = intval($keyword);
                    break;
                case 3:
                    $where['store_name'] = array('like', '%' . trim($keyword) . '%');
                    break;
            }
        }
        #筛选商品
        $store_id = session('store_id');
        $goods_id = Db::name('y_goods')->where(['store_id' => $store_id])->where(['is_del' => 0])->field('goods_commonid')->select();

        $goods_array = [];
        foreach ($goods_id as $key => $value) {
            array_push($goods_array, $value['goods_commonid']);
        }
           

        $not_goods_array = implode(',',$goods_array);
        #设置的是已经审核过的商品
        $where['goods_commonid'] = ['not in',$not_goods_array];
        $where['goods_lock'] = 0;
        $where['is_two_goods'] = 0;
        $where['goods_status'] = 1;
        $goods_list = $goods_model->getGoodsCommonOnlineListNew($where);
        // var_dump($goods_list);exit;
        //var_dump($goods_id);die;
        

        //获取分类名称
        $goods_lists = getClassName($goods_list);
        $this->assign('show_page', $goods_model->page_info->render());
        $this->assign('goods_list', $goods_lists);

        $store_id = session('store_id');

        $where = [
            'store_id' => $store_id
        ];
        $member = model('store')->where($where)->find();
        $store_join_info = db('storejoinin')->where(['member_id' => $member['member_id']])->find();
        $this->assign('member_type', $store_join_info['supplier_type']);


        // 计算库存
        $storage_array = $goods_model->calculateStorage($goods_list);
        $this->assign('storage_array', $storage_array);

        // 商品分类
        $store_goods_class = model('storegoodsclass')->getClassTree(array('store_id' => session('store_id'), 'storegc_state' => '1'));
        $this->assign('store_goods_class', $store_goods_class);

        // 商品分类  搜索用到的
        $store_goods_class = model('storegoodsclass')->getClassTree(array('store_id' => session('store_id'), 'storegc_state' => '1'));
        $this->assign('store_goods_class', $store_goods_class);

        $goods_class = model('goodsclass')->getGoodsclassLineForTag('91');
        $this->assign('goods_class', $goods_class);
        $goodsclass_model = model('goodsclass');
        //父类列表，只取到第二级
        $parent_list = $goodsclass_model->getTreeClassList(2);
        $gc_list = array();
        if (is_array($parent_list)) {
            foreach ($parent_list as $k => $v) {
                $parent_list[$k]['gc_name'] = str_repeat("&nbsp;", $v['deep'] * 2) . $v['gc_name'];
                if ($v['deep'] == 1)
                    $gc_list[$k] = $v;
            }
        }
        $this->assign('gc_list', $gc_list);

        //品牌
        $typeinfo = model('type')->getAttribute('1', session('store_id'), '91');
        list($spec_json, $spec_list, $attr_list, $brand_list) = $typeinfo;
        $this->assign('brand_list', $brand_list);

        /* 设置卖家当前菜单 */
        $this->setSellerCurMenu('importgoods');
        $this->setSellerCurItem('goods_list');
        echo $this->fetch($this->template_dir . 'store_goods_list_online');
        exit;
    }


    /**
     * 在售商品列表
     */
    public function goodslist()
    {
        $goods_model = model('goods');

        $where = array();
        $storegc_id = intval(input('get.storegc_id'));
        if ($storegc_id > 0) {
            $where['goods_stcids'] = array('like', '%,' . $storegc_id . ',%');
        }

        //筛选分类 品牌
        $b_id = intval(input('get.b_id'));
        if ($b_id != '') {
            $where['brand_id'] = intval($b_id);
        }
        $gc_id = intval(input('get.gc_id'));
        if ($gc_id != '') {
            $where["gc_id|gc_id_1|gc_id_2|gc_id_3"] = $gc_id;
        }

        $keyword = trim(input('get.keyword'));
        $search_type = trim(input('get.search_type'));

        if (trim($keyword) != '') {
            switch ($search_type) {
                case 0:
                    $where['goods_name'] = array('like', '%' . trim($keyword) . '%');
                    break;
                case 1:
                    $where['goods_serial'] = array('like', '%' . trim($keyword) . '%');
                    break;
                case 2:
                    $where['goods_commonid'] = intval($keyword);
                    break;
                case 3:
                    $where['store_name'] = array('like', '%' . trim($keyword) . '%');
                    break;
            }
        }
        $status = intval(input('get.status'));
        if ($status > 0) {
            if ($status == 1) {
                #上架
                $y_where['status'] = 0;
            } else {
                #下架
                $y_where['status'] = 1;
            }
        }
        #筛选商品
        $y_where['store_id'] = session('store_id');
        $y_where['is_del'] = 0;
        $y_where['status'] = 0;
        $goods_info = Db::name('y_goods')->where($y_where)->field('goods_commonid,price,status')->select();


        $goods_id = array_column($goods_info, 'goods_commonid');
        $goods_id = implode(',', $goods_id);
        $where['goods_commonid'] = array('in', $goods_id);
        #设置的是已经审核过的商品
        #
//        $where['goods_verify'] = 1;
        $where['goods_lock'] = 0;
        $goods_list = $goods_model->getGoodsCommonOnlineListNew($where);
        foreach ($goods_list as $k => $v) {
            foreach ($goods_info as $ke => $va) {
                if ($v['goods_commonid'] == $va['goods_commonid']) {
                    $goods_list[$k]['price'] = $va['price'];
                    $goods_list[$k]['status'] = $va['status'];
                }
            }
        }
        //获取分类名称
        $goods_lists = getClassName($goods_list);
        $this->assign('show_page', $goods_model->page_info->render());
        $this->assign('goods_list', $goods_lists);

        //获取运营商身份
        $store_id = session('store_id');

        $where = [
            'store_id' => $store_id
        ];
        $member = model('store')->where($where)->find();
        $store_join_info = db('storejoinin')->where(['member_id' => $member['member_id']])->find();
        $this->assign('member_type', $store_join_info['supplier_type']);


        // 计算库存
        $storage_array = $goods_model->calculateStorage($goods_list);
        $this->assign('storage_array', $storage_array);

        // 商品分类
        $store_goods_class = model('storegoodsclass')->getClassTree(array('store_id' => session('store_id'), 'storegc_state' => '1'));
        $this->assign('store_goods_class', $store_goods_class);

        // 商品分类  搜索用到的
        $store_goods_class = model('storegoodsclass')->getClassTree(array('store_id' => session('store_id'), 'storegc_state' => '1'));
        $this->assign('store_goods_class', $store_goods_class);

        $goods_class = model('goodsclass')->getGoodsclassLineForTag('91');
        $this->assign('goods_class', $goods_class);
        $goodsclass_model = model('goodsclass');
        //父类列表，只取到第二级
        $parent_list = $goodsclass_model->getTreeClassList(2);
        $gc_list = array();
        if (is_array($parent_list)) {
            foreach ($parent_list as $k => $v) {
                $parent_list[$k]['gc_name'] = str_repeat("&nbsp;", $v['deep'] * 2) . $v['gc_name'];
                if ($v['deep'] == 1)
                    $gc_list[$k] = $v;
            }
        }
        $this->assign('gc_list', $gc_list);

        //品牌
        $typeinfo = model('type')->getAttribute('1', session('store_id'), '91');
        list($spec_json, $spec_list, $attr_list, $brand_list) = $typeinfo;
        $this->assign('brand_list', $brand_list);

        /* 设置卖家当前菜单 */
        $this->setSellerCurMenu('goodslist');
        $this->setSellerCurItem('goods_list');
        echo $this->fetch($this->template_dir . 'store_goods_list');
        exit;
    }

    /**
     * 下架商品列表
     */
    public function goodslower()
    {
        $goods_model = model('goods');

        $where = array();
        $storegc_id = intval(input('get.storegc_id'));
        if ($storegc_id > 0) {
            $where['goods_stcids'] = array('like', '%,' . $storegc_id . ',%');
        }

        //筛选分类 品牌
        $b_id = intval(input('get.b_id'));
        if ($b_id != '') {
            $where['brand_id'] = intval($b_id);
        }
        $gc_id = intval(input('get.gc_id'));
        if ($gc_id != '') {
            $where["gc_id|gc_id_1|gc_id_2|gc_id_3"] = $gc_id;
        }

        $keyword = trim(input('get.keyword'));
        $search_type = trim(input('get.search_type'));

        if (trim($keyword) != '') {
            switch ($search_type) {
                case 0:
                    $where['goods_name'] = array('like', '%' . trim($keyword) . '%');
                    break;
                case 1:
                    $where['goods_serial'] = array('like', '%' . trim($keyword) . '%');
                    break;
                case 2:
                    $where['goods_commonid'] = intval($keyword);
                    break;
                case 3:
                    $where['store_name'] = array('like', '%' . trim($keyword) . '%');
                    break;
            }
        }
        $status = intval(input('get.status'));
        if ($status > 0) {
            if ($status == 1) {
                #上架
                $y_where['status'] = 0;
            } else {
                #下架
                $y_where['status'] = 1;
            }
        }
        #筛选商品
        $y_where['store_id'] = session('store_id');
        $y_where['is_del'] = 0;
        $y_where['status'] = 1;
        $goods_info = Db::name('y_goods')->where($y_where)->field('goods_commonid,price,status')->select();


        $goods_id = array_column($goods_info, 'goods_commonid');
        $goods_id = implode(',', $goods_id);
        $where['goods_commonid'] = array('in', $goods_id);
        #设置的是已经审核过的商品
        #
//        $where['goods_verify'] = 1;
        $where['goods_lock'] = 0;
        $goods_list = $goods_model->getGoodsCommonOnlineListNew($where);
        foreach ($goods_list as $k => $v) {
            foreach ($goods_info as $ke => $va) {
                if ($v['goods_commonid'] == $va['goods_commonid']) {
                    $goods_list[$k]['price'] = $va['price'];
                    $goods_list[$k]['status'] = $va['status'];
                }
            }
        }
        $this->assign('show_page', $goods_model->page_info->render());
        $this->assign('goods_list', $goods_list);

        //获取运营商身份
        $store_id = session('store_id');

        $where = [
            'store_id' => $store_id
        ];
        $member = model('store')->where($where)->find();
        $store_join_info = db('storejoinin')->where(['member_id' => $member['member_id']])->find();
        $this->assign('member_type', $store_join_info['supplier_type']);


        // 计算库存
        $storage_array = $goods_model->calculateStorage($goods_list);
        $this->assign('storage_array', $storage_array);

        // 商品分类
        $store_goods_class = model('storegoodsclass')->getClassTree(array('store_id' => session('store_id'), 'storegc_state' => '1'));
        $this->assign('store_goods_class', $store_goods_class);

        // 商品分类  搜索用到的
        $store_goods_class = model('storegoodsclass')->getClassTree(array('store_id' => session('store_id'), 'storegc_state' => '1'));
        $this->assign('store_goods_class', $store_goods_class);

        $goods_class = model('goodsclass')->getGoodsclassLineForTag('91');
        $this->assign('goods_class', $goods_class);
        $goodsclass_model = model('goodsclass');
        //父类列表，只取到第二级
        $parent_list = $goodsclass_model->getTreeClassList(2);
        $gc_list = array();
        if (is_array($parent_list)) {
            foreach ($parent_list as $k => $v) {
                $parent_list[$k]['gc_name'] = str_repeat("&nbsp;", $v['deep'] * 2) . $v['gc_name'];
                if ($v['deep'] == 1)
                    $gc_list[$k] = $v;
            }
        }
        $this->assign('gc_list', $gc_list);

        //品牌
        $typeinfo = model('type')->getAttribute('1', session('store_id'), '91');
        list($spec_json, $spec_list, $attr_list, $brand_list) = $typeinfo;
        $this->assign('brand_list', $brand_list);

        /* 设置卖家当前菜单 */
        $this->setSellerCurMenu('goodslist');
        $this->setSellerCurItem('goods_lower');
        echo $this->fetch($this->template_dir . 'store_goods_lower');
        exit;
    }

    #   提成运营商
    public function goods_setting_price()
    {
        if (request()->isPost()) {
            $data = input('post.');

            $where['goods_commonid'] = $data['commonids'];
            unset($data['commonids']);
            $res = Db::name('y_goods')->data($data)->where($where)->update();
            if ($res) {
                dsLayerOpenSuccess('设置分享价格成功');
            } else {
                dsLayerOpenSuccess('设置分享价格失败');
            }

        } else {
            $commonid = input('param.commonid');
            $goods = Db::name('y_goods')->where(['goods_commonid' => $commonid])->find();
            $this->view->goods = $goods;
            $this->assign('commonids', $commonid);
            return $this->fetch($this->template_dir . 'goods_setting_price');
        }
    }

    /**
     * 删除商品
     */
    public function drop_goods()
    {
        $where['goods_commonid'] = input('param.commonid');

        $goods_info = Db::name('y_goods')->where($where)->find();
        if ($goods_info['status'] == 1) {
            // 上架操作
            $data['is_del'] = 1;
            $data['del_time'] = time();
            $return = Db::name('y_goods')->data($data)->where($where)->update();

            //修改购物车商品的状态为已关闭
            $res = [
                'goods_commonid' => input('param.commonid'),
                'yun_store_id' => session('store_id')
            ];
            $cart_info = db('cart')->where($res)->select();
            if (!empty($cart_info)) {
                foreach ($cart_info as $key => $value) {
                    db('cart')->where($res)->update(['status' => 1]);
                }
            }

            if ($return) {
                ds_json_encode(10000, '删除成功');
            } else {
                ds_json_encode(10001, '删除失败');
            }

        } else {
            ds_json_encode(10001, '请将商品先下架');
        }
    }


    /*
      设置价格  加价运营商
     */
    public function cgoods_setting_price()
    {
        if (request()->isPost()) {
            $data = input('post.');

            $price = input('post.t_value/a');
            $goods_id = input('post.goods_id/a');
            $commonid = @$data['commonids'];


            foreach ($price as $k => $v) {
                $array = [];
                $array['markup_price'] = $v;
                $update_array['goods_id'] = $goods_id[$k];
                $price_info = db('goods')->where($update_array)->update($array);
            }
            //修改状态
            $common_id = $this->checkRequestCommonId($commonid);
            $commonid_array = explode(',', $common_id);
            $res = Db::name('y_goods')->where('goods_commonid', 'in', $common_id)->select();

            if (!empty($res)) {
                $y_data['is_del'] = 0;
                $y_data['del_time'] = 0;
                foreach ($res as $key => $value) {
                    Db::name('y_goods')->where(['goods_commonid' => $value['goods_commonid']])->data($y_data)->update();
                }
                foreach ($commonid_array as $k => $v) {
                    foreach ($res as $ke => $va) {
                        if ($v == $va['goods_commonid']) {
                            unset($commonid_array[$k]);
                        }
                    }
                }
            }
            $store_id = session('store_id');
            $this->recordSellerlog('加价运营商：' . $store_id . '：导入商品');

            foreach ($price as $k => $v) {
                $data[$k]['goods_commonid'] = $commonid;
                $data[$k]['goods_id'] = $goods_id[$k];
                $data[$k]['add_time'] = time();
                $data[$k]['store_id'] = session('store_id');
                $data[$k]['store_name'] = session('store_name');
                $data[$k]['price'] = $price[$k];
                $return = Db::name('y_goods')->data($data[$k])->insert();
            }

            dsLayerOpenSuccess('设置价格成功');

        } else {
            $commonid = input('param.commonid');

            $goods_info = db('goods')->alias('g')
                ->join('goodscommon gc', 'g.goods_commonid = gc.goods_commonid')
                ->field('g.*,gc.gc_name,gc.brand_name')->where(['g.goods_commonid' => $commonid])->select();


            foreach ($goods_info as $k => $v) {
                if ($v['t_type'] == 0) {
                    $goods_info[$k]['t_price'] = '未设置';
                    $goods_info[$k]['g_price'] = '未设置';
                } else {
                    if ($v['t_type'] == 1) {
                        $goods_info[$k]['t_price'] = $v['goods_price'] + $v['goods_price'] * $v['t_value'] / 100 + $v['freight'];
                    } elseif ($v['t_type'] == 2) {
                        $goods_info[$k]['t_price'] = $v['goods_price'] + $v['t_value'] + $v['freight'];
                    }
                    if ($v['g_type'] == 1) {
                        $goods_info[$k]['g_price'] = $v['goods_price'] + $v['goods_price'] * $v['g_value'] / 100 + $v['freight'];
                    } elseif ($v['g_type'] == 2) {
                        $goods_info[$k]['g_price'] = $v['goods_price'] + $v['g_value'] + $v['freight'];
                    }
                }
            }
            $this->assign('goods_info', $goods_info);
            $goods = model('Goods')->where(['goods_commonid' => $commonid])->select();

            $goodscommon = Db::name('goodscommon')->where(['goods_commonid' => $commonid])->find();
            $this->view->goods = $goods;
            $this->assign('goods', $goodscommon);

            //获取运营商身份
            $store_id = session('store_id');

            $where = [
                'store_id' => $store_id
            ];
            $member = model('store')->where($where)->find();
            $store_join_info = db('storejoinin')->where(['member_id' => $member['member_id']])->find();
            $this->assign('member_type', $store_join_info['supplier_type']);

            $this->assign('commonids', $commonid);
            $this->assign('goodscommon', $goodscommon);
            return $this->fetch($this->template_dir . 'cgoods_setting_price');
        }
    }

    /*
      加价运营商导入之后  再重新修改价格
   */
    public function ygoods_setting_price()
    {
        if (request()->isPost()) {
            $data = input('post.');

            $price = input('post.t_value/a');
            $goods_id = input('post.goods_id/a');
            $commonid = @$data['commonids'];


            foreach ($price as $k => $v) {
                $array = [];
                $array['markup_price'] = $v;
                $update_array['goods_id'] = $goods_id[$k];
                $price_info = db('goods')->where($update_array)->update($array);
            }

            foreach ($price as $k => $v) {
                $array = [];
                $array['price'] = $v;
                $array['goods_id'] = $goods_id[$k];
                $price_info = db('y_goods')->where(['goods_id' => $goods_id[$k]])->update($array);
            }

            //修改状态
            $common_id = $this->checkRequestCommonId($commonid);
            $commonid_array = explode(',', $common_id);
            $res = Db::name('y_goods')->where('goods_commonid', 'in', $common_id)->select();

            if (!empty($res)) {
                $y_data['is_del'] = 0;
                $y_data['del_time'] = 0;
                foreach ($res as $key => $value) {
                    Db::name('y_goods')->where(['goods_commonid' => $value['goods_commonid']])->data($y_data)->update();
                }
                foreach ($commonid_array as $k => $v) {
                    foreach ($res as $ke => $va) {
                        if ($v == $va['goods_commonid']) {
                            unset($commonid_array[$k]);
                        }
                    }
                }
            }
            $store_id = session('store_id');
            $this->recordSellerlog('加价运营商：' . $store_id . '：导入商品');

            dsLayerOpenSuccess('设置价格成功');

        } else {
            $commonid = input('param.commonid');
            $goods_info = db('goods')->alias('g')
                ->join('goodscommon gc', 'g.goods_commonid = gc.goods_commonid')
                ->field('g.*,gc.gc_name,gc.brand_name')->where(['g.goods_commonid' => $commonid])->select();
            // $goods_info = db('goods')->where(['goods_commonid'=>$commonid])->select();
            foreach ($goods_info as $k => $v) {
                if ($v['t_type'] == 0) {
                    $goods_info[$k]['t_price'] = '未设置';
                    $goods_info[$k]['g_price'] = '未设置';
                } else {
                    if ($v['t_type'] == 1) {
                        $goods_info[$k]['t_price'] = $v['goods_price'] + $v['goods_price'] * $v['t_value'] / 100 + $v['freight'];
                    } elseif ($v['t_type'] == 2) {
                        $goods_info[$k]['t_price'] = $v['goods_price'] + $v['t_value'] + $v['freight'];
                    }
                    if ($v['g_type'] == 1) {
                        $goods_info[$k]['g_price'] = $v['goods_price'] + $v['goods_price'] * $v['g_value'] / 100 + $v['freight'];
                    } elseif ($v['g_type'] == 2) {
                        $goods_info[$k]['g_price'] = $v['goods_price'] + $v['g_value'] + $v['freight'];
                    }
                }
            }
            // var_dump($goods_info);exit;
            $this->assign('goods_info', $goods_info);
            $goods = model('Goods')->where(['goods_commonid' => $commonid])->select();

            $goodscommon = Db::name('goodscommon')->where(['goods_commonid' => $commonid])->find();
            $this->view->goods = $goods;

            //获取运营商身份
            $store_id = session('store_id');

            $where = [
                'store_id' => $store_id
            ];
            $member = model('store')->where($where)->find();
            $store_join_info = db('storejoinin')->where(['member_id' => $member['member_id']])->find();
            $this->assign('member_type', $store_join_info['supplier_type']);


            $this->assign('commonids', $commonid);
            $this->assign('goodscommon', $goodscommon);
            return $this->fetch($this->template_dir . 'cgoods_setting_price');
        }
    }

    /*
        直接导入  提成运营商
   */
    public function import_commodity_price()
    {
        if (request()->isPost()) {
            $data = input('post.');

            //获取goodsid
            $goodsid = db('goods')->where(['goods_commonid' => $data['commonids']])->select();
            foreach ($goodsid as $k => $v) {
                $goods[] = $v['goods_id'];
            }

            $goods_id = input('post.goods_id/a');
            $commonid = @$data['commonids'];


            //修改状态
            $common_id = $this->checkRequestCommonId($commonid);
            $commonid_array = explode(',', $common_id);
            $res = Db::name('y_goods')->where('goods_commonid', 'in', $common_id)->select();

            if (!empty($res)) {
                $y_data['is_del'] = 0;
                $y_data['del_time'] = 0;
                foreach ($res as $key => $value) {
                    Db::name('y_goods')->where(['goods_commonid' => $value['goods_commonid']])->data($y_data)->update();
                }
                // 加入一个导入时间
                foreach ($res as $key => $val) {
                    $c_data['import_time'] = date('Y-m-d H:i:s', time());
                    Db::name('goodscommon')->where(['goods_commonid' => $val['goods_commonid']])->data($c_data)->update();
                }
                foreach ($commonid_array as $k => $v) {
                    foreach ($res as $ke => $va) {
                        if ($v == $va['goods_commonid']) {
                            unset($commonid_array[$k]);
                        }
                    }
                }
            }
            $store_id = session('store_id');
            $this->recordSellerlog('提成运营商：' . $store_id . '：导入商品');

            foreach ($goods_id as $k => $v) {
                $data[$k]['goods_commonid'] = $commonid;
                $data[$k]['goods_id'] = $goods_id[$k];
                $data[$k]['add_time'] = time();
                $data[$k]['store_id'] = session('store_id');
                $data[$k]['store_name'] = session('store_name');
                $return = Db::name('y_goods')->data($data[$k])->insert();
            }
            dsLayerOpenSuccess('导入成功');
        } else {
            $commonid = input('param.commonid');

            $goods_info = db('goods')->where(['goods_commonid' => $commonid])->select();


            foreach ($goods_info as $k => $v) {
                if ($v['t_type'] == 0) {
                    $goods_info[$k]['t_price'] = '未设置';
                    $goods_info[$k]['g_price'] = '未设置';
                } else {
                    if ($v['t_type'] == 1) {
                        $goods_info[$k]['t_price'] = $v['goods_price'] + $v['goods_price'] * $v['t_value'] / 100 + $v['freight'];
                    } elseif ($v['t_type'] == 2) {
                        $goods_info[$k]['t_price'] = $v['goods_price'] + $v['t_value'] + $v['freight'];
                    }
                    if ($v['g_type'] == 1) {
                        $goods_info[$k]['g_price'] = $v['goods_price'] + $v['goods_price'] * $v['g_value'] / 100 + $v['freight'];
                    } elseif ($v['g_type'] == 2) {
                        $goods_info[$k]['g_price'] = $v['goods_price'] + $v['g_value'] + $v['freight'];
                    }
                }
            }
            $this->assign('goods_info', $goods_info);
            $goods = model('Goods')->where(['goods_commonid' => $commonid])->select();

            $goodscommon = Db::name('goodscommon')->where(['goods_commonid' => $commonid])->find();
            $this->view->goods = $goods;

            $goods = db('goodscommon')->where(['goods_commonid' => $commonid])->find();
            $this->assign('goods', $goods);

            //获取运营商身份
            $store_id = session('store_id');

            $where = [
                'store_id' => $store_id
            ];
            $member = model('store')->where($where)->find();
            $store_join_info = db('storejoinin')->where(['member_id' => $member['member_id']])->find();
            $this->assign('member_type', $store_join_info['supplier_type']);


            $this->assign('commonids', $commonid);
            $this->assign('goodscommon', $goodscommon);
            return $this->fetch($this->template_dir . 'import_commodity_price');
        }
    }

    /**
     * 主商品详情页
     */
    public function store_goods_detail()
    {
        if (request()->isPost()) {
        } else {
            $config = db('config')->where('code', 'qiniu_http')->find();
            $this->assign('http_name', $config['value']);
            $commonid = input('param.commonid');
            $goods = db('goods')->where(['goods_commonid' => $commonid])->select();
            $this->assign('commonids', $commonid);
            $goods_info = db('goodscommon')->where(['goods_commonid' => $commonid])->find();
            //获取价格
            foreach ($goods as $k => $v) {
                if ($v['t_type'] == 0) {
                    $goods[$k]['t_price'] = '未设置';
                    $goods[$k]['g_price'] = '未设置';
                } else {
                    if ($v['t_type'] == 1) {
                        $goods[$k]['t_price'] = $v['goods_price'] + $v['goods_price'] * $v['t_value'] / 100 + $v['freight'];
                    } elseif ($v['t_type'] == 2) {
                        $goods[$k]['t_price'] = $v['goods_price'] + $v['t_value'] + $v['freight'];
                    }
                    if ($v['g_type'] == 1) {
                        #加价运营商
                        $goods[$k]['g_price'] = $v['goods_price'] + $v['goods_price'] * $v['g_value'] / 100 + $v['freight'];
                    } elseif ($v['g_type'] == 2) {
                        $goods[$k]['g_price'] = $v['goods_price'] + $v['g_value'] + $v['freight'];
                    }
                }
            }


            if (empty($goods_info['files'])) {
                $goods_info['files_info'] = '';
            } else {
                $goods_info['files_info'] = json_decode($goods_info['files'], true);
                $goods_info['files_info']['old_file_name'] = explode(',', $goods_info['files_info']['old_file_name']);
                $goods_info['files_info']['new_file_name'] = explode(',', $goods_info['files_info']['new_file_name']);
            }
            //获取分类名称
            $name = db('goodsclass')->where(['gc_id' => $goods_info['gc_id_3']])->find();
            $name1 = db('goodsclass')->where(['gc_id' => $goods_info['gc_id_2']])->find();
            $name2 = db('goodsclass')->where(['gc_id' => $goods_info['gc_id_1']])->find();

            if (!empty($name2) && !empty($name1) && !empty($name)) {
                $goods_info['class_name'] = $name2['gc_name'] . '>' . $name1['gc_name'] . '>' . $name['gc_name'];
            } elseif (!empty($name2) && !empty($name1)) {
                $goods_info['class_name'] = $name2['gc_name'] . '>' . $name1['gc_name'];
            } else {
                $goods_info['class_name'] = $name2['gc_name'];
            }
            //p($goods_info);die;
            $this->assign('goods_info', $goods_info);

            $goods_img = db('goodsimages')->where(['goods_commonid' => $commonid])->select();
            $this->assign('goods_img', $goods_img);

            //获取运营商身份
            $store_id = session('store_id');

            $where = [
                'store_id' => $store_id
            ];
            $member = model('store')->where($where)->find();
            $store_join_info = db('storejoinin')->where(['member_id' => $member['member_id']])->find();

            $this->assign('member_type', $store_join_info['supplier_type']);

            //获取价格

            if ($store_join_info['supplier_type'] == 1) {
                #固定加价
                foreach ($goods as $k => $v) {
                    $y_goods_info = db('y_goods')->where(['store_id' => session('store_id'), 'goods_id' => $v['goods_id']])->find();
                    if ($y_goods_info) {
                        $goods[$k]['price'] = $y_goods_info['price'].'元';
                        $goods[$k]['sum_price'] =(float)$v['g_price'] + (float)$y_goods_info['price'];
                        $goods[$k]['sum_price'] =$goods[$k]['sum_price'].'元';
                    } else {
                        $goods[$k]['price'] = '未加入';
                        $goods[$k]['sum_price'] ='未加入';
                    }
                }
            }
            $this->assign('goods', $goods);
            return $this->fetch($this->template_dir . 'store_goods_detail');
        }
    }


    /**
     * 子商品详情页
     */
    public function goods_setting_info()
    {
        if (request()->isPost()) {
        } else {
            $commonid = input('param.goods_id');
            $is_href = input('param.is_href');
            $goods = db('goods')->where(['goods_id' => $commonid])->find();
            $goods_info = db('goodscommon')->where(['goods_commonid' => $goods['goods_commonid']])->find();
            if (empty($goods['files'])) {
                $goods['files_info'] = '';
            } else {
                $goods['files_info'] = json_decode($goods['files'], true);
                $goods['files_info']['old_file_name'] = explode(',', $goods['files_info']['old_file_name']);
                $goods['files_info']['new_file_name'] = explode(',', $goods['files_info']['new_file_name']);
            }
            if ($is_href == '') {
                $goods['is_href'] = 0;

            } else {
                $goods['is_href'] = 1;
            }
            $this->assign('goods', $goods);
            $this->assign('goods_info', $goods_info);
            $config = db('config')->where('code', 'qiniu_http')->find();
            $this->assign('http_name', $config['value']);
            return $this->fetch($this->template_dir . 'goods_setting_info');
        }
    }

    /*
      设置规格实际价格
     */
    public function set_price()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $goods_commonid = @$data['commonids'];
            if (empty($data['commonids'])) {
                $this->error('操作错误');
            }
            if (!isset($data['score']) && is_numeric($data['score'])) {
                $this->error('请设置积分');
            }
            if (empty($data['status'])) {
                $this->error("操作错误11");
            }
            if (empty($data['g_value']) && is_numeric($data['g_value'])) {
                $this->error("请设置加价运营商的加价值");
            }

            if (empty($data['t_value']) && is_numeric($data['t_value'])) {
                $this->error("请设置提成运营商的加价值");
            }
            $update1['freight'] = @$data['freight'];
            $update1['score'] = @$data['score'];
            if ($data['status'] == 1) {
                $update1['install_money'] = $data['install_money'];
            } else {
                $update1['install_money'] = 0;
            }

            $update1['t_type'] = @$data['t_type'];
            $update1['g_value'] = @$data['g_value'];
            $update1['g_type'] = @$data['g_type'];
            $update1['t_value'] = @$data['t_value'];

            $is = strstr($goods_commonid, ',');
            if (empty($is)) {
                $res = db('goods')->where('goods_id', $goods_commonid)->data($update1)->update();
            } else {
                $ids = explode(',', $goods_commonid);
                foreach ($ids as $k => $v) {
                    $res = db('goods')->where('goods_id', $v)->data($update1)->update();
                }

            }

            if (!$res) {
                $this->error('设置价格失败');
            } else {
            }
            dsLayerOpenSuccess('设置价格成功');
        } else {
            $commonid = input('param.commonid');
            $is = strstr($commonid, ',');
            if (empty($is)) {
                $goods_info = db('goods')->where(['goods_id' => $commonid])->find();
                $this->assign('goods_info', $goods_info);
            }
            $this->assign('commonids', $commonid);
            return $this->fetch($this->template_dir . 'set_price');
        }
    }

    /*
      查看详情
     */
    public function goods_setting_info_jia()
    {
        if (request()->isPost()) {
        } else {
            $commonid = input('param.commonid');

            $goods_info = db('goods')->where(['goods_commonid' => $commonid])->select();

            foreach ($goods_info as $k => $v) {
                if ($v['t_type'] == 0) {
                    $goods_info[$k]['t_price'] = '未设置';
                    $goods_info[$k]['g_price'] = '未设置';
                } else {
                    if ($v['t_type'] == 1) {
                        $goods_info[$k]['t_price'] = $v['goods_price'] + $v['goods_price'] * $v['t_value'] / 100 + $v['freight'];
                    } elseif ($v['t_type'] == 2) {
                        $goods_info[$k]['t_price'] = $v['goods_price'] + $v['t_value'] + $v['freight'];
                    }
                    if ($v['g_type'] == 1) {
                        $goods_info[$k]['g_price'] = $v['goods_price'] + $v['goods_price'] * $v['g_value'] / 100 + $v['freight'];
                    } elseif ($v['g_type'] == 2) {
                        $goods_info[$k]['g_price'] = $v['goods_price'] + $v['g_value'] + $v['freight'];
                    }
                }

            }
            $this->assign('goods_info', $goods_info);
            $goods = model('Goods')->where(['goods_commonid' => $commonid])->find();

            $goodscommon = Db::name('goodscommon')->where(['goods_commonid' => $commonid])->find();
            $this->view->goods = $goods;

            $goodsinfo = model('goodscommon')->where(['goods_commonid' => $commonid])->find();
            $this->assign('goodsinfo', $goodsinfo);
            switch ($goodsinfo['change_goods']) {
                case 0:
                    $change_status = '不支持';
                    break;
                case 1:
                    $change_status = '支持';
                    break;

                default:
                    $change_status = '暂无选择';
                    break;
            }
            switch ($goodsinfo['return_goods']) {
                case 0:
                    $return_status = '不支持';
                    break;
                case 1:
                    $return_status = '支持';
                    break;

                default:
                    $return_status = '暂无选择';
                    break;
            }
            $this->assign('change_status', $change_status);
            $this->assign('return_status', $return_status);

            $where['area_id'] = array('in', $goodsinfo['area']);
            $area = model('area')->where($where)->select();
            foreach ($area as $key => $value) {
                $area_name[] = $value['area_name'];
            }
            if (empty($goodsinfo['area'])) {
                $this->assign('area', '暂无选择');
            } else {
                $name = implode(",", $area_name);

                $this->assign('area', $name);
            }

            $goods_img = db('goodsimages')->where(['goods_commonid' => $commonid])->select();
            $this->assign('goods_img', $goods_img);

            $goods_list = db('goodscommon')->where(['goods_commonid' => $commonid])->find();
            $this->assign('goods_list', $goods_list);

            $this->assign('commonids', $commonid);
            $this->assign('goodscommon', $goodscommon);
            return $this->fetch($this->template_dir . 'goods_setting_info_jia');
        }
    }

    public function goods_setting_info_ti()
    {
        if (request()->isPost()) {
        } else {
            $commonid = input('param.commonid');

            $goods_info = db('goods')->where(['goods_commonid' => $commonid])->select();

            foreach ($goods_info as $k => $v) {
                if ($v['t_type'] == 0) {
                    $goods_info[$k]['t_price'] = '未设置';
                    $goods_info[$k]['g_price'] = '未设置';
                } else {
                    if ($v['t_type'] == 1) {
                        $goods_info[$k]['t_price'] = $v['goods_price'] + $v['goods_price'] * $v['t_value'] / 100 + $v['freight'];
                    } elseif ($v['t_type'] == 2) {
                        $goods_info[$k]['t_price'] = $v['goods_price'] + $v['install_money'] + $v['t_value'] + $v['freight'];
                    }
                    if ($v['g_type'] == 1) {
                        $goods_info[$k]['g_price'] = $v['goods_price'] + $v['goods_price'] * $v['g_value'] / 100 + $v['freight'];
                    } elseif ($v['g_type'] == 2) {
                        $goods_info[$k]['g_price'] = $v['goods_price'] + $v['g_value'] + $v['freight'];
                    }
                }

            }
            $this->assign('goods_info', $goods_info);
            $goods = model('Goods')->where(['goods_commonid' => $commonid])->find();

            $goodscommon = Db::name('goodscommon')->where(['goods_commonid' => $commonid])->find();
            $this->view->goods = $goods;

            $goodsinfo = model('goodscommon')->where(['goods_commonid' => $commonid])->find();
            $this->assign('goodsinfo', $goodsinfo);
            switch ($goodsinfo['change_goods']) {
                case 0:
                    $change_status = '不支持';
                    break;
                case 1:
                    $change_status = '支持';
                    break;

                default:
                    $change_status = '暂无选择';
                    break;
            }
            switch ($goodsinfo['return_goods']) {
                case 0:
                    $return_status = '不支持';
                    break;
                case 1:
                    $return_status = '支持';
                    break;

                default:
                    $return_status = '暂无选择';
                    break;
            }
            $this->assign('change_status', $change_status);
            $this->assign('return_status', $return_status);
            $where['area_id'] = array('in', $goodsinfo['area']);
            $area = model('area')->where($where)->select();
            foreach ($area as $key => $value) {
                $area_name[] = $value['area_name'];
            }
            if (empty($goodsinfo['area'])) {
                $this->assign('area', '暂无选择');
            } else {
                $name = implode(",", $area_name);

                $this->assign('area', $name);
            }
            $goods_list = db('goodscommon')->where(['goods_commonid' => $commonid])->find();
            $this->assign('goods_list', $goods_list);

            $goods_img = db('goodsimages')->where(['goods_commonid' => $commonid])->select();
            $this->assign('goods_img', $goods_img);

            $this->assign('commonids', $commonid);
            $this->assign('goodscommon', $goodscommon);
            return $this->fetch($this->template_dir . 'goods_setting_info_ti');
        }
    }

    /**
     * 导入商品
     */
    public function add_goods()
    {
        $commonid = input('param.commonid');

        //获取goodsid
        $goodsid = db('goods')->where(['goods_commonid' => $commonid])->select();
        foreach ($goodsid as $k => $v) {
            $goods[] = $v['goods_id'];
        }
        $common_id = $this->checkRequestCommonId($commonid);

        $commonid_array = explode(',', $common_id);
        $res = Db::name('y_goods')->where('goods_commonid', 'in', $common_id)->select();


        if (!empty($res)) {
            $y_data['is_del'] = 0;
            $y_data['del_time'] = 0;
            foreach ($res as $key => $value) {
                Db::name('y_goods')->where(['goods_commonid' => $value['goods_commonid']])->data($y_data)->update();
            }
            foreach ($commonid_array as $k => $v) {
                foreach ($res as $ke => $va) {
                    if ($v == $va['goods_commonid']) {
                        unset($commonid_array[$k]);
                    }
                }
            }
        }
        foreach ($commonid_array as $k => $v) {
            $data[$k]['goods_commonid'] = $v;
            $data[$k]['goods_id'] = $goods[$k];
            $data[$k]['add_time'] = time();
            $data[$k]['store_id'] = session('store_id');
            $data[$k]['store_name'] = session('store_name');
            $result = Db::name('y_goods')->data($data[$k])->insert();
        }
        if ($result) {
            ds_json_encode(10000, '导入成功');
        } else {
            ds_json_encode(10001, '导入失败');
        }
    }

    /**
     * 商品上下架
     */
    public function goods_unshow()
    {
        $where['goods_commonid'] = input('param.commonid');
        $goods_info = Db::name('y_goods')->where($where)->find();
        if (empty($goods_info)) {
            ds_json_encode(10001, '您的商品库里没有存在此商品');
        }
        if ($goods_info['status'] == 1) {
            //操作上架的话先判断供应商是否将此商品下架
            $tag = db('goodscommon')->where(['goods_commonid' => $goods_info['goods_commonid']])->find();
            if ($tag['goods_status'] != 1) {
                ds_json_encode(10001, '该商品已无货');
            } else {
                $data['status'] = 0;
            }
        } elseif ($goods_info['status'] == 0) {
            $data['status'] = 1;

            //修改购物车商品的状态为已关闭
            $res = [
                'goods_commonid' => input('param.commonid'),
                'yun_store_id' => session('store_id')
            ];
            $cart_info = db('cart')->where($res)->select();
            if (!empty($cart_info)) {
                foreach ($cart_info as $key => $value) {
                    db('cart')->where($res)->update(['status' => 1]);
                }
            }
        }
        $return = db('y_goods')->where($where)->data($data)->update();
        if ($return) {


            ds_json_encode(10000, '操作成功');
        } else {
            ds_json_encode(10001, '操作失败');
        }

    }

    public function goods_status()
    {

        $commonid = input('param.commonid');

        $common_info = db('goodscommon')->where('store_id', session('store_id'))->where('goods_commonid', $commonid)->find();
        if (empty($common_info)) {
            ds_json_encode(10001, '您的商品库里没有存在此商品');
        }

        if ($common_info['goods_status'] == 3) {
            $data['goods_status'] = 1;
        } elseif ($common_info['goods_status'] == 1) {
            $data['goods_status'] = 3;
        }
        $return = db('goodscommon')->where('store_id', session('store_id'))->where('goods_commonid', $commonid)->data($data)->update();
        if ($return) {
            // 添加操作日志
            $this->recordSellerlog('商品下架，平台货号：' . $commonid);
            ds_json_encode(10000, '操作成功');
        } else {
            ds_json_encode(10001, '操作失败');
        }
    }

    /**
     * 设置广告词
     */
    public function edit_jingle()
    {
        if (request()->isPost()) {
            $common_id = $this->checkRequestCommonId(input('param.commonid'));
            $commonid_array = explode(',', $common_id);
            $where = array('goods_commonid' => array('in', $commonid_array), 'store_id' => session('store_id'));
            $update = array('goods_advword' => trim(input('post.g_jingle')));
            $return = model('goods')->editProducesNoLock($where, $update);
            if ($return) {
                // 添加操作日志
                $this->recordSellerlog('设置广告词，平台货号：' . $common_id);
                ds_json_encode(10000, lang('ds_common_op_succ'));
            } else {
                ds_json_encode(10001, lang('ds_common_op_fail'));
            }
        }
        $common_id = $this->checkRequestCommonId(input('param.commonid'));

        return $this->fetch($this->template_dir . 'edit_jingle');
    }

    /**
     * 设置关联版式
     */
    public function edit_plate()
    {
        if (request()->isPost()) {
            $common_id = $this->checkRequestCommonId(input('post.commonid'));
            $commonid_array = explode(',', $common_id);
            $where = array('goods_commonid' => array('in', $commonid_array), 'store_id' => session('store_id'));
            $update = array();
            $update['plateid_top'] = intval(input('post.plate_top')) > 0 ? intval(input('post.plate_top')) : '';
            $update['plateid_bottom'] = intval(input('post.plate_bottom')) > 0 ? intval(input('post.plate_bottom')) : '';
            $return = model('goods')->editGoodsCommon($update, $where);
            if ($return) {
                // 添加操作日志
                $this->recordSellerlog('设置关联版式，平台货号：' . $common_id);
                ds_json_encode(10000, lang('ds_common_op_succ'));
            } else {
                ds_json_encode(10001, lang('ds_common_op_fail'));
            }
        } else {
            $common_id = $this->checkRequestCommonId(input('param.commonid'));
            $plateid_bottom = db('goodscommon')->where(array('goods_commonid' => $common_id))->field('plateid_bottom,plateid_top')->find();
            $this->assign('plateid', $plateid_bottom);
            // 关联版式
            $plate_list = model('storeplate')->getStoreplateList(array('store_id' => session('store_id')), 'storeplate_id,storeplate_name,storeplate_position');

            $plate_list = array_under_reset($plate_list, 'storeplate_position', 2);
            $this->assign('plate_list', $plate_list);

            return $this->fetch($this->template_dir . 'edit_plate');
        }
    }

    /**
     * 添加赠品
     */
    public function add_gift()
    {
        $common_id = intval(input('param.commonid'));
        if ($common_id <= 0) {
            $this->error(lang('wrong_argument'), url('Seller/index'));
        }
        $goods_model = model('goods');
        $goodscommon_info = $goods_model->getGoodeCommonInfoByID($common_id);
        if (empty($goodscommon_info) || $goodscommon_info['store_id'] != session('store_id')) {
            $this->error(lang('wrong_argument'), url('Seller/index'));
        }

        // 商品列表
        $goods_array = $goods_model->getGoodsListForPromotion(array('goods_commonid' => $common_id), '*', 0, 'gift');
        $this->assign('goods_array', $goods_array);

        // 赠品列表
        $gift_list = model('goodsgift')->getGoodsgiftList(array('goods_commonid' => $common_id));
        $gift_array = array();
        if (!empty($gift_list)) {
            foreach ($gift_list as $val) {
                $gift_array[$val['goods_id']][] = $val;
            }
        }
        $this->assign('gift_array', $gift_array);

        /* 设置卖家当前菜单 */
        $this->setSellerCurMenu('sellergoodsonline');
        $this->setSellerCurItem('add_gift');
        return $this->fetch($this->template_dir . 'store_goods_edit_add_gift');
    }

    /**
     * 保存赠品
     */
    public function save_gift()
    {
        if (!request()->isPost()) {
            ds_json_encode(10001, lang('wrong_argument'));
        }
        $gift_array = input('post.gift/a');
        $commonid = intval(input('param.commonid'));
        if ($commonid <= 0) {
            ds_json_encode(10001, lang('wrong_argument'));
        }

        $goods_model = model('goods');
        $goodsgift_model = model('goodsgift');

        // 验证商品是否存在
        $goods_list = $goods_model->getGoodsListForPromotion(array('goods_commonid' => $commonid, 'store_id' => session('store_id')), 'goods_id', 0, 'gift');
        if (empty($goods_list)) {
            ds_json_encode(10001, lang('wrong_argument'));
        }
        // 删除该商品原有赠品
        $goodsgift_model->delGoodsgift(array('goods_commonid' => $commonid));
        // 重置商品礼品标记
        $goods_model->editGoods(array('is_have_gift' => 0), array('goods_commonid' => $commonid));
        // 商品id
        $goodsid_array = array();
        foreach ($goods_list as $val) {
            $goodsid_array[] = $val['goods_id'];
        }

        $insert = array();
        $update_goodsid = array();
        foreach ($gift_array as $key => $val) {

            $owner_gid = intval($key);  // 主商品id
            // 验证主商品是否为本店铺商品,如果不是本店商品继续下一个循环
            if (!in_array($owner_gid, $goodsid_array)) {
                continue;
            }
            $update_goodsid[] = $owner_gid;
            foreach ($val as $k => $v) {
                $gift_gid = intval($k); // 礼品id
                // 验证赠品是否为本店铺商品，如果不是本店商品继续下一个循环
                $gift_info = $goods_model->getGoodsInfoByID($gift_gid);
                $is_general = $goods_model->checkIsGeneral($gift_info);     // 验证是否为普通商品
                if ($gift_info['store_id'] != session('store_id') || $is_general == false) {
                    continue;
                }

                $array = array();
                $array['goods_id'] = $owner_gid;
                $array['goods_commonid'] = $commonid;
                $array['gift_goodsid'] = $gift_gid;
                $array['gift_goodsname'] = $gift_info['goods_name'];
                $array['gift_goodsimage'] = $gift_info['goods_image'];
                $array['gift_amount'] = intval($v);
                $insert[] = $array;
            }
        }
        // 插入数据
        if (!empty($insert))
            $goodsgift_model->addGoodsgiftAll($insert);
        // 更新商品赠品标记
        if (!empty($update_goodsid)) {
            $goods_model->editGoodsById(array('is_have_gift' => 1), $update_goodsid);
        }
        ds_json_encode(10000, lang('ds_common_save_succ'));
    }

    /**
     * 推荐搭配
     */
    public function add_combo()
    {
        $common_id = intval(input('param.commonid'));
        if ($common_id <= 0) {
            $this->error(lang('wrong_argument'), url('Seller/index'));
        }
        $goods_model = model('goods');
        $goodscommon_info = $goods_model->getGoodeCommonInfoByID($common_id);
        if (empty($goodscommon_info) || $goodscommon_info['store_id'] != session('store_id')) {
            $this->error(lang('wrong_argument'), url('Seller/index'));
        }

        $goods_array = $goods_model->getGoodsListForPromotion(array('goods_commonid' => $common_id), '*', 0, 'combo');
        $this->assign('goods_array', $goods_array);

        // 推荐组合商品列表
        $combo_list = model('goodscombo')->getGoodscomboList(array('goods_commonid' => $common_id));
        $combo_goodsid_array = array();
        if (!empty($combo_list)) {
            foreach ($combo_list as $val) {
                $combo_goodsid_array[] = $val['combo_goodsid'];
            }
        }

        $combo_goods_array = $goods_model->getGeneralGoodsList(array('goods_id' => array('in', $combo_goodsid_array)), 'goods_id,goods_name,goods_image,goods_price');
        $combo_goods_list = array();
        if (!empty($combo_goods_array)) {
            foreach ($combo_goods_array as $val) {
                $combo_goods_list[$val['goods_id']] = $val;
            }
        }

        $combo_array = array();
        foreach ($combo_list as $val) {
            $combo_array[$val['goods_id']][] = $combo_goods_list[$val['combo_goodsid']];
        }
        $this->assign('combo_array', $combo_array);


        /* 设置卖家当前菜单 */
        $this->setSellerCurMenu('sellergoodsonline');
        $this->setSellerCurItem('add_combo');
        return $this->fetch($this->template_dir . 'store_goods_edit_add_combo');
    }

    /**
     * AJAX获取商品分类
     */
    public function ajax_class()
    {
        $id = intval(input('param.id'));
        $deep = intval(input('param.deep'));
        if ($id <= 0 || $deep <= 0 || $deep >= 4) {
            echo 'false';
            exit();
        }
        $deep += 1;
        $goodsclass_model = model('goodsclass');

        // 验证分类是否存在
        $gc_info = $goodsclass_model->getGoodsclassInfoByIds($id);
        if (empty($gc_info)) {
            echo 'false';
            exit();
        }

        // 读取商品分类
        if ($deep != 4) {
            $gc_list = $goodsclass_model->getGoodsclass($this->_store_id, $id, $deep);
        }
        // 分类不为空输出分类信息
        if (!empty($gc_list)) {
            $data = array('type' => 'class', 'data' => $gc_list, 'deep' => $deep);
        } else {
            // 查询类型
            $type_model = model('type');
            $spec_list = $type_model->getSpecByType(array('type_id' => $gc_info['type_id']), 't.type_id, s.*');

            $data = array('type' => 'spec', 'data' => $spec_list, 'gcid' => $id, 'aa' => $gc_info, 'gcid' => $id, 'deep' => $deep);
        }

        echo json_encode($data);
        exit();
    }

    /**
     * 保存赠品
     */
    public function save_combo()
    {
        if (!request()->isPost()) {
            ds_json_encode(10001, lang('wrong_argument'));
        }

        $combo_array = input('post.combo/a');#获取数组
        if (!isset($combo_array)) {
            ds_json_encode(10001, lang('wrong_argument'));
        }

        $commonid = intval(input('param.commonid'));
        if ($commonid <= 0) {
            ds_json_encode(10001, lang('wrong_argument'));
        }

        $goods_model = model('goods');
        $goodscombo_model = model('goodscombo');

        // 验证商品是否存在
        $goods_list = $goods_model->getGoodsListForPromotion(array('goods_commonid' => $commonid, 'store_id' => session('store_id')), 'goods_id', 0, 'combo');
        if (empty($goods_list)) {
            ds_json_encode(10001, lang('wrong_argument'));
        }
        // 删除该商品原有赠品
        $goodscombo_model->delGoodscombo(array('goods_commonid' => $commonid));
        // 商品id
        $goodsid_array = array();
        foreach ($goods_list as $val) {
            $goodsid_array[] = $val['goods_id'];
        }

        $insert = array();
        if (!empty($combo_array)) {
            foreach ($combo_array as $key => $val) {

                $owner_gid = intval($key);  // 主商品id
                // 验证主商品是否为本店铺商品,如果不是本店商品继续下一个循环
                if (!in_array($owner_gid, $goodsid_array)) {
                    continue;
                }
                $val = array_unique($val);
                foreach ($val as $v) {
                    $combo_gid = intval($v); // 礼品id
                    // 验证推荐组合商品是否为本店铺商品，如果不是本店商品继续下一个循环
                    $combo_info = $goods_model->getGoodsInfoByID($combo_gid);
                    $is_general = $goods_model->checkIsGeneral($combo_info);     // 验证是否为普通商品
                    if ($combo_info['store_id'] != session('store_id') || $is_general == false || $owner_gid == $combo_gid) {
                        continue;
                    }

                    $array = array();
                    $array['goods_id'] = $owner_gid;
                    $array['goods_commonid'] = $commonid;
                    $array['combo_goodsid'] = $combo_gid;
                    $insert[] = $array;
                }
            }
            // 插入数据
            $goodscombo_model->addGoodscomboAll($insert);
        }
        ds_json_encode(10000, lang('ds_common_save_succ'));
    }

    /**
     * 搜索商品（添加赠品/推荐搭配)
     */
    public function search_goods()
    {
        $where = array();
        $where['store_id'] = session('store_id');
        $name = input('param.name');
        if ($name) {
            $where['goods_name'] = array('like', '%' . $name . '%');
        }
        $goods_model = model('goods');
        $goods_list = $goods_model->getGeneralGoodsList($where, '*', 5);
        $this->assign('show_page', $goods_model->page_info->render());
        $this->assign('goods_list', $goods_list);
        echo $this->fetch($this->template_dir . 'store_goods_edit_search_goods');
        exit;
    }

    /**
     * 下载F码
     */
    public function download_f_code_excel()
    {
        $common_id = input('param.commonid');
        if ($common_id <= 0) {
            $this->error(lang('wrong_argument'));
        }
        $common_info = model('goods')->getGoodeCommonInfoByID($common_id);
        if (empty($common_info) || $common_info['store_id'] != session('store_id')) {
            $this->error(lang('wrong_argument'));
        }
        //import('excels.excel',EXTEND_PATH);
        $excel_obj = new \excel\Excel();
        $excel_data = array();
        //设置样式
        $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
        //header
        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('goodsfcode_code'));
        $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('goodsfcode_state'));
        $data = model('goodsfcode')->getGoodsfcodeList(array('goods_commonid' => $common_id));
        foreach ($data as $k => $v) {
            $tmp = array();
            $tmp[] = array('data' => $v['goodsfcode_code']);
            $tmp[] = array('data' => $v['goodsfcode_state'] ? lang('goodsfcode_state_1') : lang('goodsfcode_state_0'));
            $excel_data[] = $tmp;
        }
        $excel_data = $excel_obj->charset($excel_data, CHARSET);
        $excel_obj->addArray($excel_data);
        $excel_obj->addWorksheet($excel_obj->charset($common_info['goods_name'], CHARSET));
        $excel_obj->generateXML($excel_obj->charset($common_info['goods_name'], CHARSET) . '-' . date('Y-m-d-H', time()));
    }

    /**
     * 验证commonid
     */
    private function checkRequestCommonId($common_ids)
    {
        if (!preg_match('/^[\d,]+$/i', $common_ids)) {
            ds_json_encode(10001, lang('param_error'));
        }
        return $common_ids;
    }

    /**
     * ajax获取商品列表
     */
    public function get_goods_list_ajax()
    {
        $common_id = input('param.commonid');
        if ($common_id <= 0) {
            echo 'false';
            exit();
        }
        $goods_model = model('goods');
        $goodscommon_list = $goods_model->getGoodeCommonInfoByID($common_id);
        if (empty($goodscommon_list) || $goodscommon_list['store_id'] != session('store_id')) {
            echo 'false';
            exit();
        }
        $goods_list = $goods_model->getGoodsList(array('store_id' => session('store_id'), 'goods_commonid' => $common_id), 'goods_id,goods_spec,store_id,goods_price,goods_serial,goods_storage_alarm,goods_storage,goods_image');
        if (empty($goods_list)) {
            echo 'false';
            exit();
        }

        $spec_name = array_values((array)unserialize($goodscommon_list['spec_name']));
        foreach ($goods_list as $key => $val) {
            $goods_spec = array_values((array)unserialize($val['goods_spec']));
            $spec_array = array();
            foreach ($goods_spec as $k => $v) {
                $spec_array[] = '<div class="goods_spec">' . $spec_name[$k] . lang('ds_colon') . '<em title="' . $v . '">' . $v . '</em>' . '</div>';
            }
            $goods_list[$key]['goods_image'] = goods_thumb($val, '240');
            $goods_list[$key]['goods_spec'] = implode('', $spec_array);
            $goods_list[$key]['alarm'] = ($val['goods_storage_alarm'] != 0 && $val['goods_storage'] <= $val['goods_storage_alarm']) ? 'style="color:red;"' : '';
            $goods_list[$key]['url'] = url('Goods/index', ['goods_id' => $val['goods_id']]);
        }

        echo json_encode($goods_list);
    }

    /**
     *    栏目菜单
     */
    function getSellerItemList()
    {
        $item_list = array(
            array(
                'name' => 'goods_list',
                'text' => '导入商品',
                'url' => url('Importgoods/index'),
            ),
        );
        if (request()->action() === 'goodslist' || request()->action() === 'goodslower') {
            $item_list = array(
                array(
                    'name' => 'goods_list',
                    'text' => '在售商品',
                    'url' => url('Importgoods/goodslist'),
                ), array(
                    'name' => 'goods_lower',
                    'text' => '下架商品',
                    'url' => url('Importgoods/goodslower'),
                ),
            );
        }

        return $item_list;
    }

}

?>
